package cn.iocoder.yudao.module.system.controller.admin.tenantdetail;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.tenantdetail.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.tenantdetail.TenantDetailDO;
import cn.iocoder.yudao.module.system.convert.tenantdetail.TenantDetailConvert;
import cn.iocoder.yudao.module.system.service.tenantdetail.TenantDetailService;

@Tag(name = "管理后台 - 店铺详情")
@RestController
@RequestMapping("/system/tenant-detail")
@Validated
public class TenantDetailController {

    @Resource
    private TenantDetailService tenantDetailService;

    @PostMapping("/create")
    @Operation(summary = "创建店铺详情")
    @PreAuthorize("@ss.hasPermission('system:tenant-detail:create')")
    public CommonResult<Long> createTenantDetail(@Valid @RequestBody TenantDetailCreateReqVO createReqVO) {
        return success(tenantDetailService.createTenantDetail(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新店铺详情")
    @PreAuthorize("@ss.hasPermission('system:tenant-detail:update')")
    public CommonResult<Boolean> updateTenantDetail(@Valid @RequestBody TenantDetailUpdateReqVO updateReqVO) {
        tenantDetailService.updateTenantDetail(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除店铺详情")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:tenant-detail:delete')")
    public CommonResult<Boolean> deleteTenantDetail(@RequestParam("id") Long id) {
        tenantDetailService.deleteTenantDetail(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得店铺详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:tenant-detail:query')")
    public CommonResult<TenantDetailRespVO> getTenantDetail(@RequestParam("id") Long id) {
        TenantDetailDO tenantDetail = tenantDetailService.getTenantDetail(id);
        return success(TenantDetailConvert.INSTANCE.convert(tenantDetail));
    }

    @GetMapping("/list")
    @Operation(summary = "获得店铺详情列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('system:tenant-detail:query')")
    public CommonResult<List<TenantDetailRespVO>> getTenantDetailList(@RequestParam("ids") Collection<Long> ids) {
        List<TenantDetailDO> list = tenantDetailService.getTenantDetailList(ids);
        return success(TenantDetailConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得店铺详情分页")
    @PreAuthorize("@ss.hasPermission('system:tenant-detail:query')")
    public CommonResult<PageResult<TenantDetailRespVO>> getTenantDetailPage(@Valid TenantDetailPageReqVO pageVO) {
        PageResult<TenantDetailDO> pageResult = tenantDetailService.getTenantDetailPage(pageVO);
        return success(TenantDetailConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出店铺详情 Excel")
    @PreAuthorize("@ss.hasPermission('system:tenant-detail:export')")
    @OperateLog(type = EXPORT)
    public void exportTenantDetailExcel(@Valid TenantDetailExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<TenantDetailDO> list = tenantDetailService.getTenantDetailList(exportReqVO);
        // 导出 Excel
        List<TenantDetailExcelVO> datas = TenantDetailConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "店铺详情.xls", "数据", TenantDetailExcelVO.class, datas);
    }

    @GetMapping("/get-bytenantid")
    @Operation(summary = "获得店铺详情")
    @Parameter(name = "tenantId", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:tenant-detail:query')")
    public CommonResult<TenantDetailRespVO> getTenantDetailByTenantId(@RequestParam("tenantId") Long tenantId) {
        TenantDetailDO tenantDetail = tenantDetailService.getTenantDetailByTenantId(tenantId);
        return success(TenantDetailConvert.INSTANCE.convert(tenantDetail));
    }

}
